const puppeteer = require('puppeteer-core');
const config = require('./config/config.json');

class AuthBrowser {

   instance;
  _broswer;
  constructor(browser) {
    this._broswer = browser;
  }

  static async getInstance() {
    if(false === this.instance instanceof this) {
      const browser = await puppeteer.launch(config.puppeteerConfig);
      const {consoleDomain,cookieCompletedUrl, loginUrl, username, password} = config.consoleAuthConfig;
      const authUrl = `${consoleDomain}${loginUrl}`;
      const page = await browser.newPage();
      await page.goto(authUrl);
      
      const inputElHandler = await page.waitForSelector('#personalAccountInputId input[type=text]');
      await inputElHandler.type(username);
      await page.waitForTimeout(200);

      const pwdElHandler = await page.waitForSelector('#personalPasswordInputId input[type=password]');
      await pwdElHandler.type(password);
      await page.waitForTimeout(50);
      await pwdElHandler.press('Enter');

      await page.waitForNavigation({
        waitUntil: 'networkidle0'
      });

      await page.goto(`${consoleDomain}${cookieCompletedUrl}`);

      await page.waitForNavigation({
        'waitUntil': 'networkidle2'
      })

      this.instance =  new AuthBrowser(browser);
    }

    return AuthBrowser.instance;
  }
}

class Xin1 {
  _broswer;
  _page;
  constructor(browser, endpoint) {
    this._broswer = browser;
    this._endpoint = endpoint;
  }

  async run() {
    this._page = await this._broswer.newPage();

    this._page.on("response", async (response) => {
      await this.checkCf2(response);
    });

    await this._page.goto(this._endpoint);
    console.log('isCF@:::::', this.isCf2);

    await this._page.waitForNavigation({
      timeout: 0,
      waitUntil: 'networkidle2'
    })
    console.log('abc');
    const allElements = await this._page.$$eval('*', tags => tags.map(t => t.tagName));
    const tiElements = allElements.filter(tagName => /^ti-/i.test(tagName));
    console.log("aaalllll:::", tiElements);
    // await this.checkTinyUI();

  }
  async checkTinyUI() {
    await this._page.waitForNavigation({
      timeout: 0,
      waitUntil: 'networkidle2'
    })
    console.log('abc');
    const allElements = await this._page.$$eval('*', tags => tags.map(t => t.tagName));
    const tiElements = allElements.filter(tagName => /^ti-/i.test(tagName));
    console.log("aaalllll:::", tiElements);
  }


  isCf2 = false;
  async checkCf2(response) {
    const request = response.request();
    const url = request.url();
    const resourceType = request.resourceType();

    if(url.startsWith(this._endpoint) && resourceType === 'document') {
      console.log(`url====>${url}`);
      const headers = response.headers();
      const entryItem = Object.entries(headers).find(([key, value])=> {
        return key.toLowerCase() === 'x-consoleframe-flag' && ['J-CF2', 'J-CF2.0'].includes(value);
      })
      if(entryItem) {
        this.isCf2 = true;
      }
    }
    return this.isCf2;
  }

}

(async () => {
  const browser = await AuthBrowser.getInstance();
  
  const task = new Xin1(browser._broswer, 'https://console.huaweicloud.com/das');
  task.run();
  
})();